/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author tho.hoang
 */
public class RadioGroupItem extends AListItem {

    private ListItemCollections items = new ListItemCollections();
    private int selectedIndex = 0;
    private int textWidth, paddingLeft = 0, textHeight, itemsHeight;

    public RadioGroupItem(String id) {
        super("", null);
        super.id = id;
        this.style.setPadding(new Padding(4));
    }

    public RadioGroupItem(String id, String text) {
        super(text, null);
        super.id = id;
        this.style.setPadding(new Padding(4));
    }

    public void addItem(String id, String text) {
        items.addElement(new RadioItem(id, text), this.parent);
    }

    public void removeAt(int index) {
        items.removeElementAt(index);
    }

    public int getItemsSize() {
        return items.size();
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }
        // pre-calc
        calculateHeight();
        setTextRows(text);

        // paint background
        g.setColor(focus == false ? style.getBgColor() : style.getSelectedBgColor());
        g.fillRect(x, y, width, height);

        // paint symbol
        //paintUncheckSymbol(x + style.getPadding().getTop(), y + style.getPadding().getLeft(), g);
        //if (checked) {
        //    paintCheckedSymbol(x + style.getPadding().getTop(), y + style.getPadding().getLeft(), g);
        //}

        // paint text
        if (focus) {
            for (int i = 0; i < textRows.length; i++) {
                font.getFontNormal().drawString(g, items.size() + textRows[i], x + style.getPadding().getLeft() + paddingLeft, y + style.getPadding().getTop() + i * (font.getFontNormal().getHeight() + style.getPadding().getTopAndBottom()), Graphics.TOP | Graphics.LEFT);
            }
        } else {
            font.getFontNormal().drawString(g, textRows[0], x + style.getPadding().getLeft() + paddingLeft, y + style.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);
        }
    }

    public void setText(String text) {
        this.text = text;
        setTextRows(text);
    }

    protected void calculateHeight() {
        int textH = 0, iconH = 0;

        if (icon != null) {
            iconH = icon.getHeight();
        }

        if (textRows == null) {
            setTextRows(text);
        }
        itemsHeight = 0;
        if (items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                itemsHeight += ((RadioItem) items.elementAt(i)).getHeight();
            }
        }

        textH = iconH > textHeight ? iconH : textHeight;

        if (focus) {
            this.height = textH + itemsHeight;
        } else {
            this.height = textH;
        }
    }

    protected void setTextRows(String text) {
        paddingLeft = style.getPadding().getLeft();
        if (icon != null) {
            paddingLeft = icon.getWidth() + style.getPadding().getLeft();
        }

        textWidth = width - paddingLeft;

        if (text.length() > 0) {
//            textRows = boldFont.splitWith(text, textWidth);
            textRows = new String[]{text};
            textHeight = textRows.length * (font.getFontBold().getHeight() + style.getPadding().getTopAndBottom());
        } else {
            textRows = new String[]{""};
            textHeight = 0;
        }
    }

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        for (int i = 0; i < items.size(); i++) {
            ((AListItem) items.elementAt(i)).setParent(parent);
        }
    }
}
